# -*- encoding: utf-8 -*-
"""
File Name: dedicated_line_monitor_pipeline
Description: Implements the DedicatedLineMonitorPipeline class for monitoring dedicated lines.
Author : 'lirui'
date: 10/24/2024
Contact: marjey_lee@163.com
Change Activity:
10/24/2024:
"""
import ll
from projects.monitoring.branch_dedicated_line_monitor.services.load_chongqing_branch_line_status import LoadChongQingBranchLineStatus
from projects.monitoring.branch_dedicated_line_monitor.services.load_yunnan_branch_line_status import LoadYunnanBranchLineStatus
from projects.monitoring.branch_dedicated_line_monitor.utils.load_cmdb_info import load_branch_dedicated_info_from_cmdb
from projects.monitoring.common.do.alarm import Alarm
from projects.monitoring.common.utils.redis_keys_util import RedisKeysUtil
from projects.monitoring.common.workflow.monitoring_pipline_interface import MonitorPipLineInterface


class BranchDedicatedLineMonitorPipeline(MonitorPipLineInterface):
    """
    A class to monitor dedicated lines by checking the connectivity of routers.

    Attributes:
        _MONITORING_TYPE (str): The type of monitoring.
        results (dict): A dictionary to store the results of the ping checks.
    """
    _MONITORING_TYPE = RedisKeysUtil.get_branch_dedicated_line_monitor_publisher_keys()

    def __init__(self, monitor_interval=20):
        """
        Initializes the DedicatedLineMonitorPipeline with the monitoring type and router connection mapping.
        """
        super().__init__(self._MONITORING_TYPE, monitor_interval)
        self.results = {}

    def _load_original_alarms(self):
        """
        Loads the original alarms by checking the connectivity of routers.

        Returns:
            None
        """
        error_ips = []
        for cls in [LoadChongQingBranchLineStatus, LoadYunnanBranchLineStatus]:
            monitor = cls()
            monitor()
            for k, v in monitor.results.items():
                self.results[k] = v
            error_ips = error_ips + monitor.error_ips
        return error_ips

    def _format_alarms(self, error_ips) -> list[Alarm]:
        """
        Abstract method to format alarms. Must be implemented by subclasses.

        Args:

        Returns:
            list: A list of formatted alarms.
        """
        ret = []
        cmdb_info = load_branch_dedicated_info_from_cmdb()
        for ip in error_ips:
            if ip == '10.5.157.10':
                continue
            if ip in cmdb_info:
                branch_name = cmdb_info.get(ip)
            else:
                branch_name = 'unknown'
            alarm = Alarm(
                timestamp=ll.TimeUtil.get_current_timestamp(),
                ip=ip,
                hostname=branch_name,
                severity='warning',
                alarm_type='alarm',
                alarm_info=f"【分支行专线异常】{branch_name}-{ip}",
                recovery=False,
                manager_ip='专线汇聚路由器',
                manager_port=0,
                alarm_source='分支行专线告警'
            )
            ret.append(alarm)
        return ret
